<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商户号" prop="mchNo" v-if="checkPermi(['platop'])">
        <el-input
          v-model="queryParams.mchNo"
          placeholder="请输入商户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="商户名称" prop="mchName" v-if="checkPermi(['platop'])">-->
<!--        <el-input-->
<!--          v-model="queryParams.mchName"-->
<!--          placeholder="请输入商户名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="商户订单号" prop="mchOrderNo">
        <el-input
          v-model="queryParams.mch_order_no"
          placeholder="请输入商户订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder=""
          clearable
        >
          <el-option
            v-for="dict in dict.type.payorder_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="回调状态" prop="notifyState">
        <el-select
          v-model="queryParams.notifyState"
          placeholder=""
          clearable
        >
          <el-option
            v-for="dict in dict.type.notify_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <br/>
      <el-form-item label="起始时间" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.$ge_createTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.$le_createTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <!--        <el-button-->
        <!--          type="warning"-->
        <!--          plain-->
        <!--          icon="el-icon-download"-->
        <!--          size="mini"-->
        <!--          @click="handleExport"-->
        <!--          v-hasPermi="['pay:order:query']"-->
        <!--        >导出-->
        <!--        </el-button>-->
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <!--      <el-table-column label="系统订单号" align="center" prop="id"/>-->
      <el-table-column v-if="checkPermi(['platop'])" label="商户" align="center" width="200">
        <template slot-scope="scope">
          <p>
            <span style="color: rgb(114, 158, 213); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">商户号</span>
            <span>{{ scope.row.mchNo }}</span>
          </p>
          <p>
            <span style="color: rgb(86, 207, 86); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">名称</span>
            <span>{{ scope.row.mchName }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" width="250">
        <template slot-scope="scope">
          <p v-if="checkPermi(['platop'])">
           <span style="color: rgb(114, 158, 213); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">平台</span>
            <span>{{ scope.row.id }}</span>
          </p>
          <p>
            <span v-if="checkPermi(['platop'])"  style="color: rgb(86, 207, 86); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">商户</span>
            <span>{{ scope.row.mchOrderNo }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="支付通道" align="center" prop="ifName"/>
      <!--      <el-table-column label="支付方式" align="center" prop="wayCode"/>-->
      <el-table-column label="支付金额" align="center" prop="amount" width="220">
        <template slot-scope="scope">
          <template v-if="scope.row.isDigtal == 0">
            {{ scope.row.amount|amountFilter }}
          </template>
          <template v-else>
            <p>
              <span style="color: rgb(114, 158, 213); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">法币:</span>
            </p>
            <p>
              <span>{{ scope.row.amount|amountFilter }} {{ scope.row.currency }}</span>
            </p>
            <p>
              <span style="color: rgb(86, 207, 86); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">数字货币:</span>
            </p>
            <p>
              <span>{{ scope.row.digtalAmount|amountFilter}} {{scope.row.digtalCurrency}}</span>
            </p>
          </template>
        </template>
      </el-table-column>
      <!--      <el-table-column v-if="checkPermi(['platop'])" label="通道费率%" align="center" prop="ifRate"/>-->
      <el-table-column v-if="checkPermi(['platop'])" label="通道手续费" align="center" prop="ifFeeAmount">
        <template slot-scope="scope">
          {{ scope.row.ifFeeAmount|amountFilter }}
        </template>
      </el-table-column>
      <!--      <el-table-column label="费率%" align="center" prop="mchFeeRate"/>-->
      <el-table-column label="手续费" align="center" prop="mchFeeAmount">
        <template slot-scope="scope">
          {{ scope.row.mchFeeAmount|amountFilter }}
        </template>
      </el-table-column>
      <el-table-column label="货币代码" align="center">
        <template slot-scope="scope">
          <el-tag v-if="checkPermi(['platop'])">
            {{ scope.row.platCurrency }}
          </el-tag>
          <el-tag v-else>
            {{ scope.row.currency }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.payorder_state" :value="scope.row.state" v-if="scope.row.state != 3">
          </dict-tag>
          <div v-else style="width: 100%;display: flex;justify-content: center;align-items: center;">
            <el-tooltip v-if="scope.row.state == 3" class="item" effect="dark" :content="scope.row.errMsg"
                        placement="top-start">
              <el-tag type="danger" style="height: 32px; display: flex;justify-content: center;align-items: center;">
                失败
              </el-tag>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="回调状态" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.notify_state" :value="scope.row.notifyState" v-if="scope.row.notifyState != 2">
          </dict-tag>
          <div v-else style="width: 100%;display: flex;justify-content: center;align-items: center;">
            <el-tooltip v-if="scope.row.notifyState == 2" class="item" effect="dark" :content="scope.row.errMsg"
                        placement="top-start">
              <el-tag type="danger" style="height: 32px; display: flex;justify-content: center;align-items: center;">
                失败
              </el-tag>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="支付成功时间" align="center" prop="successTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.successTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-phone"
            @click="manualNotify(scope.row)">
          手动回调
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-if="checkPermi(['pay:order:force']) && scope.row.state <= 1 "
            icon="el-icon-phone"
            @click="manualForce(scope.row, true)">
            强制成功
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-if="checkPermi(['pay:order:force'])  && scope.row.state <= 1"
            icon="el-icon-phone"
            @click="manualForce(scope.row, false)">
            强制失败
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {listOrder, getOrder, delOrder, addOrder, updateOrder, notify, forceFail, forceSuccess} from "@/api/fusion/payorder";
import {checkPermi} from "../../../../utils/permission";
import {delRecord} from "@/api/fusion/mchrecord";

export default {
  dicts: ['payorder_state', 'notify_state'],
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        isAsc: 'desc',
        orderByColumn: 'id',
        pageNum: 1,
        pageSize: 10,
        mchNo: null,
        parentNo: null,
        mchName: null,
        mchOrderNo: null,
        ifCode: null,
        wayCode: null,
        amount: null,
        ifRate: null,
        ifFeeAmount: null,
        mchFeeRate: null,
        mchFeeAmount: null,
        currency: null,
        state: null,
        notifyState: null,
        clientIp: null,
        subject: null,
        body: null,
        channelExtra: null,
        channelUser: null,
        channelOrderNo: null,
        refundState: null,
        refundTimes: null,
        refundAmount: null,
        divisionMode: null,
        divisionState: null,
        divisionLastTime: null,
        errCode: null,
        errMsg: null,
        extParam: null,
        notifyUrl: null,
        returnUrl: null,
        expiredTime: null,
        successTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mchNo: [
          {required: true, message: "商户号不能为空", trigger: "blur"}
        ],
        mchName: [
          {required: true, message: "商户名称不能为空", trigger: "blur"}
        ],
        mchOrderNo: [
          {required: true, message: "商户订单号不能为空", trigger: "blur"}
        ],
        wayCode: [
          {required: true, message: "支付方式代码不能为空", trigger: "blur"}
        ],
        amount: [
          {required: true, message: "支付金额,单位分不能为空", trigger: "blur"}
        ],
        mchFeeRate: [
          {required: true, message: "商户手续费费率快照不能为空", trigger: "blur"}
        ],
        mchFeeAmount: [
          {required: true, message: "商户手续费,单位分不能为空", trigger: "blur"}
        ],
        currency: [
          {required: true, message: "三位货币代码,人民币:cny不能为空", trigger: "blur"}
        ],
        state: [
          {
            required: true,
            message: "支付状态: 0-订单生成, 1-支付中, 2-支付成功, 3-支付失败, 4-已撤销, 5-已退款, 6-订单关闭不能为空",
            trigger: "blur"
          }
        ],
        notifyState: [
          {required: true, message: "向下游回调状态, 0-未发送,  1-已发送不能为空", trigger: "blur"}
        ],
        subject: [
          {required: true, message: "商品标题不能为空", trigger: "blur"}
        ],
        body: [
          {required: true, message: "商品描述信息不能为空", trigger: "blur"}
        ],
        refundState: [
          {required: true, message: "退款状态: 0-未发生实际退款, 1-部分退款, 2-全额退款不能为空", trigger: "blur"}
        ],
        refundTimes: [
          {required: true, message: "退款次数不能为空", trigger: "blur"}
        ],
        refundAmount: [
          {required: true, message: "退款总金额,单位分不能为空", trigger: "blur"}
        ],
        notifyUrl: [
          {required: true, message: "异步通知地址不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  filters: {
    amountFilter(val) {
      return (parseFloat(val) / 100).toFixed(2)
    }
  },
  created() {
    this.getList();
  },
  methods: {
    checkPermi,
    /** 查询支付订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mchNo: null,
        parentNo: null,
        mchName: null,
        mchOrderNo: null,
        ifCode: null,
        wayCode: null,
        amount: null,
        ifRate: null,
        ifFeeAmount: null,
        mchFeeRate: null,
        mchFeeAmount: null,
        currency: null,
        state: null,
        notifyState: null,
        clientIp: null,
        subject: null,
        body: null,
        channelExtra: null,
        channelUser: null,
        channelOrderNo: null,
        refundState: null,
        refundTimes: null,
        refundAmount: null,
        divisionMode: null,
        divisionState: null,
        divisionLastTime: null,
        errCode: null,
        errMsg: null,
        extParam: null,
        notifyUrl: null,
        returnUrl: null,
        expiredTime: null,
        successTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付订单";
    },
    /** 修改按钮操作 */
    manualNotify(row) {
      // this.reset();
      // const id = row.id || this.ids
      notify(row.id).then(response => {
        if(response.code == 200) {
          this.$modal.msgSuccess("回调成功");
        } else {
          this.$modal.msgError("回调失败:" + response.msg);
        }
      });
    },
    manualForce(row, state) {
      this.$modal.confirm('是否进行强制行为').then(function() {
      }).then(() => {
        if(state) {
          forceSuccess(row.id).then(response => {
            if(response.code == 200) {
              this.$modal.msgSuccess("强制成功");
              row.state = 2
            } else {
              this.$modal.msgError("回调失败:" + response.msg);
            }
          });
        } else {
          forceFail(row.id).then(response => {
            if(response.code == 200) {
              row.state = 3
              this.$modal.msgSuccess("强制失败");
            } else {
              this.$modal.msgError("回调失败:" + response.msg);
            }
          });
        }
      }).catch(() => {});
      // this.reset();
      // const id = row.id || this.ids

    },
    /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateOrder(this.form).then(response => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addOrder(this.form).then(response => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pay/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
